package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {

    public static final String EMP_IN_SESSION = "EMP_IN_SESSION";
    private static final String EXPRESSIONS_IN_SESSION = "EXPRESSIONS_IN_SESSION";

    // 获取当前线程的请求对象
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }

    // 获取当前线程的 session 对象
    public static HttpSession getSession(){
        HttpServletRequest request = getRequest();
        return request.getSession();
    }

    // 往 session 存员工对象
    public static void setEmployee(Employee e){
        getSession().setAttribute(EMP_IN_SESSION, e);
    }

    // 从 session 取员工对象
    public static Employee getEmployee(){
        return ((Employee) getSession().getAttribute(EMP_IN_SESSION));
    }


    public static void setExpressions(List<String> expressions) {
        getSession().setAttribute(EXPRESSIONS_IN_SESSION, expressions);
    }

    // 从 session 取权限表达式
    public static List<String> getExpressions(){

        return ((List<String>) getSession().getAttribute(EXPRESSIONS_IN_SESSION));
    }
}
